package user;

import book.Book;
import book.Library;
import book.PairOfUidAndBookId;
import com.bit.utils.FileUtils;
import constant.Constant;
import utils.AnalyzingBook;
import utils.AnalyzingBorrowedBook;
import utils.LibrarySingleton;

import java.util.Scanner;

import static constant.Constant.BORROW_ALL_BOOK_FILE_NAME;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 87111
 * Date: 2025-08-21
 * Time: 12:54
 */
public class NormalUser extends User {
    //普通用户借阅图书的相关信息
    private PairOfUidAndBookId[] pairOfUidAndBookId;
    //实际借阅量
    private int borrowTrueNum;
    //最多借阅图书的数量
    private static final int BORROW_MAX_BOOK_NUM = 5;
    //图书借阅信息有关的工具类
    private final AnalyzingBorrowedBook analyzingBorrowedBook = new AnalyzingBorrowedBook();
    Scanner sc = new Scanner(System.in);
    Library library = LibrarySingleton.createLibrary();
    public NormalUser(String userName, int userId) {
        super(userName, userId, "普通用户");

    }

    //从文件中读取借书记录
    private void readBorrowBook() {
        PairOfUidAndBookId[] allBorrowBook;
        //加载文件中的借阅信息
        allBorrowBook = analyzingBorrowedBook.readBorrowBook(Constant.BORROW_ALL_BOOK_FILE_NAME);
        //默认已借阅的图书的数组大小为BORROW_MAX_BOOK_NUM
        pairOfUidAndBookId = new PairOfUidAndBookId[BORROW_MAX_BOOK_NUM];
        //如果借阅记录为空，则实际借阅量为0
        if(allBorrowBook == null) {
            borrowTrueNum = 0;
        }else {
            int len = allBorrowBook.length;
            if(len > BORROW_MAX_BOOK_NUM) {
                pairOfUidAndBookId = new PairOfUidAndBookId[len];
            }
            for (int i = 0; i < len; i++) {
                pairOfUidAndBookId[i] = allBorrowBook[i];
            }
            borrowTrueNum = len;
        }
    }
    //将借书数据写入文件中
    private void writeBorrowBook() {
        analyzingBorrowedBook.writeBorrowBook(pairOfUidAndBookId, Constant.BORROW_ALL_BOOK_FILE_NAME);
    }
    public int showMenu() {
        System.out.println("普通用户" + userName + "的操作菜单");
        System.out.println("1.查找图书");
        System.out.println("2.打印所有的图书");
        System.out.println("3.退出系统");
        System.out.println("4.借阅图书");
        System.out.println("5.归还图书");
        System.out.println("6.查看当前个人借阅情况");
        System.out.println("请选择你的操作：");
        return sc.nextInt();
    }
    //借阅图书
    public void borrowBook() {
        library.displayBooks();
        sc.nextLine();
        System.out.println("请输入你要借阅图书的id：");
        int bookId = sc.nextInt();

        //如果书架没有书不能借阅
        if(library.getBookCount() == 0) {
            System.out.println("书架没有图书");
            return;
        }

        //判断借的书是否存在
        Book book = library.searchBook(bookId);
        if(book == null) {
            System.out.println("图书馆没有你要借的书");
            return;
        }

        //读取图书借阅信息
        readBorrowBook();

        //判断该书是否被自己或者其他人借过
        for (int i = 0; i < borrowTrueNum; i++) {
            PairOfUidAndBookId[] pair = pairOfUidAndBookId;
            if(pair[i].getBookId() == bookId) {
                if(pair[i].getUserId() == this.getUserId()) {
                    System.out.println("该书你已经借阅过");
                    return;
                }else {
                    System.out.println("该书被其他用户借阅过,用户id为：" + pair[i].getUserId());
                    return;
                }
            }
        }

        //借书
        library.borrowBook(bookId);

        //加载借阅记录到文件中
        PairOfUidAndBookId pair = new PairOfUidAndBookId(userId, bookId);
        pairOfUidAndBookId[borrowTrueNum] = pair;
        borrowTrueNum++;

        //存储数据到文件中
        writeBorrowBook();
        System.out.println("借书成功");
    }
    //归还图书
    public void returnBook() {
        readBorrowBook();
        viewBorrowBook();
        System.out.println("请输入你要归还图书的id：");
        int bookId = sc.nextInt();
        for (int i = 0; i < borrowTrueNum; i++) {
            if (pairOfUidAndBookId[i].getBookId() == bookId) {
                if (pairOfUidAndBookId[i].getUserId() == getUserId()) {
                    library.returnBook(bookId);
                    System.out.println("图书归还成功");
                    pairOfUidAndBookId[i] = pairOfUidAndBookId[borrowTrueNum - 1];
                    pairOfUidAndBookId[borrowTrueNum - 1] = null;
                    borrowTrueNum--;
                    writeBorrowBook();
                }else {
                    System.out.println("该书籍不是你借阅的书");
                }
            }
        }
        System.out.println("没有你需要归还的书");
    }

    //查看当前个人借阅情况
    public void viewBorrowBook() {
        readBorrowBook();
        System.out.println("你的借阅情况如下：");
        if(borrowTrueNum == 0) {
            System.out.println("你没有借过书");
        }else {
            for (int i = 0; i < borrowTrueNum; i++) {
                if(pairOfUidAndBookId[i].getUserId() == userId) {
                    Book book = library.searchBook(pairOfUidAndBookId[i].getBookId());
                    System.out.println(book);
                }
            }
        }
    }
}
